<template>
  <el-container class="flow-info-container">
    <el-main>
      <!-- 已办任务 -->
      <div style="position: relative;">
        <el-form ref="formMyApprovedTask" :model="formFilter" label-width="100px" :size="defaultFormItemSize" label-position="right" @submit.native.prevent>
          <FilterBox :item-width="350">
            <el-form-item label="流程名称" prop="processDefinitionName">
              <el-input
                v-model="formFilter.processDefinitionName"
                class="filter-item"
                :clearable="true"
                placeholder="流程名称"
              />
            </el-form-item>
            <el-form-item label="发起时间" prop="createDate">
              <date-range
                v-model="formFilter.createDate"
                class="filter-item"
                :clearable="true"
                :allow-types="['day']"
                align="left"
                range-separator="-"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
                format="yyyy-MM-dd"
                value-format="yyyy-MM-dd HH:mm:ss"
              />
            </el-form-item>
            <el-button slot="operator" type="default" :plain="true" :size="defaultFormItemSize" @click="onReset">重置</el-button>
            <el-button slot="operator" type="primary" :plain="true" :size="defaultFormItemSize" @click="refreshFormMyApprovedTask(true)">查询</el-button>
          </FilterBox>
        </el-form>
        <el-row>
          <el-col :span="24">
            <el-table
              ref="teacher"
              border
              :header-cell-style="{background:'#f4f4f4', color:'#717171' }"
              :data="handlerTaskWidget.dataList"
              :size="defaultFormItemSize"
              header-cell-class-name="table-header-gray"
              @sort-change="handlerTaskWidget.onSortChange"
            >
              <el-table-column label="序号" header-align="center" align="center" type="index" width="55px" :index="handlerTaskWidget.getTableIndex" />
              <el-table-column label="流程名称" prop="processDefinitionName" />
              <el-table-column label="流程标识" prop="processDefinitionKey" />
              <el-table-column label="任务名称" prop="name" />
              <el-table-column label="执行操作">
                <template slot-scope="scope">
                  <span>{{ SysFlowTaskOperationType.getValue(scope.row.approvalType) }}</span>
                </template>
              </el-table-column>
              <el-table-column label="任务发起人" prop="startUser" />
              <el-table-column label="任务发起时间" prop="createTime" />
              <el-table-column label="操作" width="100px">
                <template slot-scope="scope">
                  <el-button class="table-btn success" :size="defaultFormItemSize" type="text" @click="onTaskDetail(scope.row)">详情</el-button>
                </template>
              </el-table-column>
            </el-table>
            <el-row type="flex" justify="end" style="margin-top: 10px;">
              <el-pagination
                :total="handlerTaskWidget.totalCount"
                :current-page="handlerTaskWidget.currentPage"
                :page-size="handlerTaskWidget.pageSize"
                :page-sizes="[10, 20, 50, 100]"
                layout="total, prev, pager, next, sizes"
                @current-change="handlerTaskWidget.onCurrentPageChange"
                @size-change="handlerTaskWidget.onPageSizeChange"
              />
            </el-row>
          </el-col>
        </el-row>
      </div>
    </el-main>
  </el-container>
</template>

<script>
import '@/staticDict/flowStaticDict.js'
/* eslint-disable-next-line */
import { TableWidget } from '@/utils/widget.js'
/* eslint-disable-next-line */
import { uploadMixin, statsDateRangeMixin, cachePageMixin } from '@/core/mixins'
import { FlowOperationController } from '@/api/flowController.js'
import FilterBox from '@/components/FilterBox'
import '@/components/Dialog'

export default {
  name: 'FormMyApprovedTask',
  components: {
    FilterBox
  },
  mixins: [uploadMixin, statsDateRangeMixin, cachePageMixin],
  props: {
  },
  data() {
    return {
      formFilter: {
        processDefinitionName: undefined,
        createDate: []
      },
      formFilterCopy: {
        processDefinitionName: undefined,
        createDate: []
      },
      handlerTaskWidget: new TableWidget(this.loadHandlerTaskData, this.loadHandlerTaskVerify, true, false),
      isInit: false
    }
  },
  mounted() {
    // 初始化页面数据
    this.formInit()
  },
  methods: {
    onReset() {
      this.$refs.formMyApprovedTask.resetFields()
      this.refreshFormMyApprovedTask(true)
    },
    /**
     * 获取已办任务列表
     */
    loadHandlerTaskData(params) {
      if (params == null) params = {}
      params = {
        ...params,
        processDefinitionName: this.formFilterCopy.processDefinitionName,
        beginDate: this.formFilterCopy.createDate[0],
        endDate: this.formFilterCopy.createDate[1]
      }
      return new Promise((resolve, reject) => {
        FlowOperationController.listHistoricTask(params).then(res => {
          resolve({
            dataList: res.data.dataList.map(item => {
              const formInfo = JSON.parse(item.formKey)
              return {
                ...item,
                formInfo
              }
            }),
            totalCount: res.data.totalCount
          })
        }).catch(e => {
          reject(e)
        })
      })
    },
    /**
     * 校验已办任务过滤参数
     */
    loadHandlerTaskVerify() {
      this.formFilterCopy.processDefinitionName = this.formFilter.processDefinitionName
      this.formFilterCopy.createDate = Array.isArray(this.formFilter.createDate) ? [...this.formFilter.createDate] : []
      return true
    },
    /**
     * 刷新已办任务列表
     */
    refreshFormMyApprovedTask(reloadData = false) {
      if (reloadData) {
        this.handlerTaskWidget.refreshTable(true, 1)
      } else {
        this.handlerTaskWidget.refreshTable()
      }
    },
    /**
     * 详情
     */
    onTaskDetail(row) {
      const params = {
        taskId: row.id,
        processDefinitionId: row.processDefinitionId,
        processInstanceId: row.processInstanceId
      }

      FlowOperationController.viewHistoricTaskInfo(params).then(res => {
        this.$router.push({
          name: res.data.routerName || 'handlerFlowTask',
          query: {
            processDefinitionKey: row.processDefinitionKey,
            taskId: row.id,
            processInstanceId: row.processInstanceId,
            processDefinitionId: row.processDefinitionId,
            formId: (row.formInfo || {}).formId,
            routerName: row.routerName,
            readOnly: true,
            flowEntryName: row.processDefinitionName,
            processInstanceInitiator: row.processInstanceInitiator,
            // 在已办理任务中仅显示加签和撤销操作
            operationList: res.data.operationList.filter(item => {
              return item.type === this.SysFlowTaskOperationType.CO_SIGN || item.type === this.SysFlowTaskOperationType.REVOKE
            })
          }
        })
      }).then(res => {})
    },
    onResume() {
      this.refreshFormMyApprovedTask()
    },
    initFormData() {
    },
    formInit() {
      this.refreshFormMyApprovedTask()
    }
  }
}
</script>

<style  lang="scss">
  .flow-info-container {
    display: flex;

    flex-direction: column;
    margin: 0 auto;
    .blueLink {
      color: #409eff;
      font-size: 14px;
      margin-left: 20px;
      cursor: pointer;
    }
    .btnList {
      margin-top:20px;
      display: flex;
      justify-content: right;
      float: right;
      .submitBtn {
        margin-right: 30px;
      }
    }
    .fromFlex {
      height: 36px;
      display: flex;
      align-items: center;
    }
    .base-info {
      display: flex;
      margin-bottom: 20px;
      .basics-info {
        width: 100%;
      }
    }
  }
</style>
